/*

	This file constructs a product-level data set containing different product groupings

*/


cd ${work}4_reg_variables

use input\export_ipdt, clear
collapse (sum) vrd, by(vare year) fast

*CONCORDE PRODUCT GROUPS
destring vare, gen(cn8) force
drop if cn8==.
merge 1:1 cn8 year using input\cn8_cn8plus_2002_2015, keepusing(cn8plus) keep(3) nogen
drop cn8

*AGGREGATED PRODUCT GROUPS CONSTANT WITHIN CN8+
foreach n of numlist 2 4 {
	
	gen cn`n'0=substr(vare,1,`n')
	
	bysort cn8plus cn`n'0: egen temp=total(vrd)
	gsort cn8plus -temp
	by cn8plus: gen cn`n'=cn`n'0 if _n==1
	replace cn`n'=cn`n'[_n-1] if cn`n'=="" & _n>1
	drop temp cn`n'0

}

*BEC PRODUCT GROUPS
rename vare CN	
merge m:1 CN using input\bec_p, keepusing(BEC) assert(2 3) keep(3) nogen
rename CN vare

gen group=""
replace group="Capital" if group=="" & ( inlist(BEC,"521") | inlist(substr(BEC,1,2),"41") )
replace group="Intermediate" if group=="" & ( inlist(BEC,"111","121","322") | inlist(substr(BEC,1,2),"21","22","31","42","53") )
replace group="Consumption" if group=="" & ( inlist(BEC,"112","122","522") | inlist(substr(BEC,1,2),"61","62","63") )	
drop BEC

rename group group0
bysort cn8plus group0: egen temp=total(vrd)
gsort cn8plus -temp
by cn8plus: gen group=group0 if _n==1
by cn8plus: replace group=group[_n-1] if _n>1
drop temp group0

*SAVE
keep cn8plus cn2 cn4 group
duplicates drop

rename cn8plus vnr
rename group bec_group

compress
save output\productcodes_p, replace